/*=====================================================================================================================
                    Copyright(c) 2009 Geometric PLM Software Corp. All rights reserved.
                             Unpublished - All rights reserved
=======================================================================================================================
File description:
    Filename: string_utils.c
    Module  : Common module.

        This file includes some operations of the string.

=======================================================================================================================
Date               Name              Description of Change
14-Jul-2009        Penn,Xie          Initialize creation
02-Sep-2009        Penn,Xie          Change _itoa with sprintf, not use in AIX(XLC)
23-Sep-2009        Penn,Xie          Remove GSTR_divideString_w
$HISTORY$
=====================================================================================================================*/
#ifndef _cplusplus
#define _CRT_SECURE_NO_DEPRECATE
#endif

#ifdef BUILD_IMAN
#include <iman_string.h>
#include <iman_util.h>

#define  GTC_strcmp          iman_strcmp
#define  GTC_strncpy         iman_strncpy
#define  GTC_strcpy          iman_strcpy
#define  GTC_strlen          iman_strlen
#define  GTC_strcat          iman_strcat
#define  GTC_strstr          iman_strstr

#else

#include <fclasses\tc_string.h>
#include <tc\tc_util.h>

#define  GTC_strcmp          tc_strcmp
#define  GTC_strncpy         tc_strncpy
#define  GTC_strcpy          tc_strcpy
#define  GTC_strlen          tc_strlen
#define  GTC_strcat          tc_strcat
#define  GTC_strstr          tc_strstr

#endif

#include <base_utils\mem.h>
#include <tccore\workspaceobject.h>
#include <stdlib.h>
#include <ctype.h>

#include "string_utils.h"

char* GSTR_clone( char **dst, const char *src )
{
    char *retVal = NULL;
    int  srcLen  = 0;
    
    *dst = NULL;
    if (src == NULL)
        return NULL;

    srcLen = (int)GTC_strlen( src ) + 1;
    *dst   = (char*)MEM_alloc( srcLen * sizeof(char) );
    retVal = GTC_strncpy( *dst, src, srcLen );
    (*dst)[srcLen - 1] = '\0';

    return retVal;
}

char* GSTR_copy( char *dst, const char *src, int dstSize )
{
    char *retVal = GTC_strncpy( dst, src, dstSize );
    dst[dstSize - 1] = '\0';
    return retVal;
}

char* GSTR_int_to_string( char **dst, int value )
{
    char strVal[WSO_name_size_c + 1];

    *dst = NULL;
    memset( strVal, 0, sizeof(strVal)/sizeof(char) );
    sprintf( strVal, "%d", value );

    return GSTR_clone( dst, strVal );
}

void GSTR_format_int_to_string( char *dst, int digitNum, int value )
{
    char sNum[WSO_desc_size_c + 1];
    sprintf( sNum, "%%0%dd", digitNum );
    sprintf( dst, sNum, value );
}


char* GSTR_string_append( const char *s1, const char *s2 )
{
    char *s = NULL;
    if (s1 == NULL || s2 == NULL)
    {
        GSTR_clone(&s, s1 == NULL ? (s2 == NULL ? "" : s2) : s1 );
    }
    else
    {
        int size = (int)GTC_strlen(s1) + (int)GTC_strlen(s2) + 1;
        s = (char *)MEM_alloc( size );
        GTC_strcpy( s, s1 );
        GTC_strcat( s, s2 );
        s[size - 1] = '\0';
    }
    return s;
}

logical GSTR_string_to_int(const char *strValue, int *intValue)
{
    if (strValue == NULL)
        return false;

    *intValue = atoi(strValue);
    if (*intValue == 0)
    {
        if(GTC_strcmp(strValue, "0")!=0 && GTC_strcmp(strValue, "+0")!=0 && GTC_strcmp(strValue, "-0")!=0)
        {
            return false;
        }
    }
    else if(*intValue == INT_MAX)
    {
        if(GTC_strcmp(strValue, "2147483647")!=0 && GTC_strcmp(strValue, "+2147483647")!=0)
        {
            return false;
        }
    }
    else if(*intValue == INT_MIN)
    {
        if(GTC_strcmp(strValue, "-2147483648")!=0)
        {
            return false;
        }
    }
    return true;
}

logical GSTR_is_float(const char *str)
{
    logical isfloat = true;
    char *pStr = (char *)str;
    logical hasPositive = false;
    logical hasMinus    = false;
    logical hasDot      = false;

    if (str == NULL)
        return false;

    while (*pStr != '\0' && isfloat == true)
    {
        if ( (*pStr >= '0' && *pStr <= '9'))
        {
            //continue;
        }
        else if ( *pStr == '+' )
        {
            isfloat = (hasPositive ? false : (hasPositive = true));
        }
        else if ( *pStr == '-' )
        {
            isfloat = (hasMinus ? false : (hasMinus = true));
        }
        else if ( *pStr == '.' )
        {
            isfloat = (hasDot ? false : (hasDot = true));
        }
        else
            isfloat = false;

        pStr ++;
    }
    return isfloat;
}

int GSTR_trim_l( char *str, char s )
{
    int count     = 0;
    char *pointer = str, *poffset = NULL;
    if (str == NULL || str[0] == '\0')
        return 0;

    while ( *pointer != '\0' )
    {
        if ( *pointer != s )
        {
            break;
        }
        count++;
        pointer++;
    }
    if (count == 0)
        return 0;

    poffset = str + count;
    pointer = str;
    while ( *poffset != '\0' )
    {
        *pointer = *poffset;
        pointer ++;
        poffset ++;
    }
    *pointer = '\0';

    return count;
}

int GSTR_trim_r( char *str, char s )
{
    int count = 0;
    char *pointer = NULL;
    if (str == NULL || str[0] == '\0')
        return 0;

    pointer = str + ((int) strlen(str) - 1);

    while ( pointer != str )
    {
        if ( *pointer != s )
        {
            break;
        }

        *pointer = '\0';

        count++;
        pointer--;
    }

    return count;
}

void GSTR_trim_float( char *floatValue )
{
    if ( !IS_EMPTY(floatValue) && GTC_strstr(floatValue, ".") != NULL )
    {
        int len = 0;
        GSTR_trim_r(floatValue, '0');
        len = (int)GTC_strlen(floatValue);
        if (floatValue[ len - 1 ] == '.')
            floatValue[ len - 1 ] = '\0';
    }
}

char * GSTR_replace_str(char *str)
{
	int i=0;
	int len=0;
	char *tostr;
	tostr = (char*)malloc(sizeof(char)*(WSO_name_size_c+1));
	len = strlen(str);
	for(i=0;i<len;i++)
	{
		if(str[i]=='\n'||str[i]==' ')
		{
			tostr[i]=';';
		}
		else
		{
		tostr[i]=str[i];
		}
	}
	tostr[len]='\0';
	return(tostr);
}

